home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / gfx / lise2.1 / lise / src / jcampio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-06  |  6.7 KB  |  278 lines

  1. /* Convert JCAMP-DX spectrum to LISE
  2. */
  3.  
  4. #include <stdio.h>
  5. #include <spec.h>
  6.  
  7. #define TLN10 0.434294482
  8. #define LN10   2.30258509
  9.  
  10. float x,y,*spc,*err,*tim;
  11. float yfactor, xfactor;
  12. char EOL[4];
  13.  
  14. help()
  15. {
  16.   printf("jcampio spectrum [-l2j] [-j2l] [-nocr]\n");
  17.   printf("  converts spectra from LISE to JCAMP and vice versa\n");
  18.   printf("  options:\n");
  19.   printf("     -l2j     convert from LISE to JCAMP\n");
  20.   printf("     -j2l     convert from JCAMP to LISE\n");
  21.   printf("     -yf fpnr divide Y-values by factor on jcamp output\n");
  22.   printf("     -xf fpnr divide X-values by factor on jcamp output\n");
  23.   printf("     -nocr    suppress CR before LF\n");
  24.   return(0);
  25. }
  26.  
  27. main(argc,argv)
  28. int argc;
  29. char *argv[];
  30. {
  31. int n,i,max,memsiz;
  32. char z[80],comment[80];
  33.  
  34.    yfactor = 0.0;
  35.    xfactor = 0.0;
  36.  
  37.    memsiz = (_MAXSPCLEN+2) * sizeof(float);
  38.    spc= (float *)malloc(memsiz);
  39.    err= (float *)malloc(memsiz);
  40.    tim= (float *)malloc(memsiz);
  41.    if(tim==NULL) {
  42.       printf("sorry, not enough memory\n");
  43.       exit(-1);
  44.    }
  45.    EOL[0]=13; EOL[1]=10; EOL[2]=0;
  46.    if(checkopt(argc,argv,"-nocr",z)) {
  47.       EOL[0]=10; EOL[1]=0;
  48.    }
  49.    if(checkopt(argc,argv,"-yf",z)) {
  50.       yfactor = atosf(z);
  51.    }
  52.    if(checkopt(argc,argv,"-xf",z)) {
  53.       xfactor = atosf(z);
  54.    }
  55.    if(checkopt(argc,argv,"-l2j",z)) {
  56.       max=readspec(argv[1],spc,err,tim,comment);
  57.       strcpy(z,argv[1]); n = strlen(z);
  58.       for(i = 0; i < n; i++) if(z[i] == '.') z[i] = 0;
  59.       writejcamp(z,spc,err,_utim,max,comment);  /* we use "_utim" as a Bugfix for the SAS-C */
  60.    }
  61.  
  62.    if(checkopt(argc,argv,"-j2l",z)) {
  63.       max = readjcamp(argv[1],spc,err,tim,comment);
  64.       strcpy(z,argv[1]); n = strlen(z);
  65.       for(i = 0; i < n; i++) if(z[i] == '.') z[i] = 0;
  66.       writespec(z,spc,err,max,2,comment);
  67.       if(_spc_onam[0] != 0) strcpy(z,_spc_onam);
  68.       strcat(z,".tim");
  69.       writespec(z,tim,err,max,2,comment);
  70.    }
  71.    free(err); free(spc); free(tim);
  72.    exit(0);
  73. }
  74.  
  75. float getnumber(s)
  76. char *s;
  77. {
  78. char z[80];
  79. int i,n,l;
  80. float erg;
  81.  
  82.    n = 0; l = strlen(s);
  83.    erg = 0.0;
  84.    while(s[n++] != '=') if(n >= l) return(0.0);
  85.    i = 0; while(s[n] > 31) z[i++] = s[n++];
  86.    z[i] = 0;
  87.    erg = atosf(z);
  88.    return(erg);
  89. }
  90.  
  91. float calcfactor(max,min)
  92. float max,min;
  93. {
  94. float y;
  95. int imax,imin;
  96. int i;
  97.  
  98.    if(max < 0.0) max = -1.0 * max;
  99.    if(min < 0.0) min = -1.0 * min;
  100.  
  101.    imax = (int) (log(max) * TLN10);
  102.    imin = (int) (log(min) * TLN10);
  103.    i = imax;
  104.    if((i > 60) || (i < -60)) i = imin;
  105.    if((i > 60) || (i < -60)) return(1.0);
  106.    y = exp(LN10 * i);
  107.    y = 1.0 / (y * 10000.0);
  108.    return(1.0); /* makes some trouble */
  109. }
  110.  
  111. writejcamp(name,spc,err,tim,max,comment)
  112. char *name, *comment;
  113. float *spc, *err, *tim;
  114. int max;
  115. {
  116. FILE *fp;
  117. int i,n,m;
  118. char *s, *z;
  119. float maxy, miny;
  120. float maxx, minx;
  121.  
  122.    s = (char *) malloc(256);
  123.    z = (char *) malloc(256);
  124.  
  125.    sprintf(s,"%s.jcamp",name);
  126.    fp = fopen(s,"w");
  127.    if(fp == NULL) {
  128.       fprintf(stderr,"jcampio: could not open >%s< for write\n",s);
  129.       free(s);
  130.       return(0);
  131.    }
  132.  
  133.    maxy = spc[0]; miny = spc[0];
  134.    maxx = tim[0]; minx = tim[0];
  135.    for(n = 0; n < max; n++) {
  136.       if(spc[n] > maxy) maxy = spc[n];
  137.       if(spc[n] < miny) miny = spc[n];
  138.       if(tim[n] > maxx) maxx = tim[n];
  139.       if(tim[n] < minx) minx = tim[n];
  140.    }
  141.  
  142.    if(xfactor == 0.0) xfactor = calcfactor(maxx,minx);
  143.    if(yfactor == 0.0) yfactor = calcfactor(maxy,miny);
  144.  
  145.    fprintf(fp,"##TITLE=%s%s",comment,EOL);
  146.    fprintf(fp,"##JCAMP-DX=4.24%s",EOL);
  147.    fprintf(fp,"##DATA TYPE= LISE%s",EOL);
  148.    fprintf(fp,"##ORIGIN= (C) 1993 by Rainer Kowallik%s",EOL);
  149.    fprintf(fp,"##OWNER= Public Domain%s",EOL);
  150.    fprintf(fp,"##XUNITS= arbitrary%s",EOL);
  151.    fprintf(fp,"##YUNITS= arbitrary%s",EOL);
  152.    fpfmt(s,xfactor);
  153.    fprintf(fp,"##XFACTOR=%s%s",s,EOL);
  154.    fpfmt(s,yfactor);
  155.    fprintf(fp,"##YFACTOR=%s%s",s,EOL);
  156.    fpfmt(s,tim[0]);
  157.    fprintf(fp,"##FIRSTX=%s%s",s,EOL);
  158.    fpfmt(s,tim[max-1]);
  159.    fprintf(fp,"##LASTX=%s%s",s,EOL);
  160.    fprintf(fp,"##NPOINTS=%d%s",max,EOL);
  161.    fpfmt(s,spc[0]);
  162.    fprintf(fp,"##FIRSTY=%s%s",s,EOL);
  163.    fpfmt(s,maxy);
  164.    fprintf(fp,"##MAXY=%s%s",s,EOL);
  165.    fpfmt(s,miny);
  166.    fprintf(fp,"##MINY=%s%s",s,EOL);
  167.    fpfmt(s,_tica);
  168.    fprintf(fp,"##DELTAX=%s%s",s,EOL);
  169.    fprintf(fp,"##XYDATA=(X++(Y..Y))%s",EOL);
  170.    n = 0;
  171.    while(n < max) {
  172.       sprintf(z,"%d",(int)(tim[n] / xfactor));
  173.       while(strlen(z) < 70) {
  174.          sprintf(s,"%+d",(int)(spc[n] / yfactor));
  175.          strcat(z,s);
  176.          if((n++) >= max) break;
  177.       }
  178.       fprintf(fp,"%s%s",z,EOL);
  179.    }
  180.    fprintf(fp,"##END=%s",EOL);
  181.    fclose(fp);
  182.  
  183.    free(z); free(s);
  184. }
  185.  
  186. fpfmt(s,x)
  187. char *s;
  188. float x;
  189. {
  190. double y;
  191. int i;
  192.  
  193.    y = (double) x;
  194.    i = (int) x;
  195.    sprintf(s,"%g",y);
  196.    return(0);
  197. }
  198.  
  199. readjcamp(name,spc,err,tim,comment)
  200. char *name, *comment;
  201. float *spc, *err, *tim;
  202. {
  203. FILE *fp;
  204. int i,n,l,max;
  205. char *s, *z;
  206. float firstx, deltax, yfactor;
  207.  
  208.    s = (char *) malloc(256); z = (char *) malloc(256);
  209.  
  210.    firstx = 0.0; deltax = 1.0;
  211.    yfactor = 1.0;
  212.    strcpy(comment,"no comment");
  213.  
  214.    strcpy(s,name);
  215.    fp = fopen(s,"r");
  216.    if(fp == NULL) {
  217.       strcat(s,".jcamp");
  218.       fp = fopen(s,"r");
  219.       if(fp == NULL) {
  220.          fprintf(stderr,"jcampio: could not open >%s< for read\n",name);
  221.          free(s); free(z);
  222.          return(0);
  223.       }
  224.    }
  225.    while(!feof(fp)) {
  226.       fgets(s,200,fp);
  227.       if(instr("TITLE",s) > 0) {
  228.          n = 0; i = 0;
  229.          while(s[i++] != '=') if(i > 100) break;
  230.          while(s[i] > 30) comment[n++] = s[i++];
  231.          comment[n] = 0;
  232.       }
  233.       if(instr("FIRSTX",s) > 0) firstx = getnumber(s);
  234.       if(instr("DELTAX",s) > 0) {
  235.          deltax = getnumber(s);
  236.          _tica = deltax;
  237.       }
  238.       if(instr("YFACTOR",s) > 0) yfactor = getnumber(s);
  239.       if(instr("XYDATA",s) > 0) break;
  240.    }
  241.    max = 0;
  242.  
  243.    while(!feof(fp)) {
  244.       fgets(s,200,fp);
  245.       n = 0; while(s[n] > 20) n++;
  246.       s[n] = 0;
  247.       if(s[0] == '#') continue;
  248.       if(strlen(s) < 3) break;
  249.       if(instr("END",s) > 0) break;
  250.       n = 0; l = strlen(s);
  251.       while(s[n] < 42) n++;           /* skip blanks and sign of X*/
  252.       while(s[n++] > 45) if(n >= l) break; /* first skip X */
  253.       while( 1 == 1) {
  254.          i = 0; if(n >= l) break;
  255.          n = n - 1;
  256.          while(s[n] == 32) n++;           /* skip blanks */
  257.          z[i++] = s[n++];                 /* read sign */
  258.          while(s[n] > 45) z[i++] = s[n++];
  259.          z[i] = 0; n++;
  260.          spc[max] = ((float) atoi(z)) * yfactor;
  261.          err[max] = 0.001 * spc[max]; /* you'll need an error for the fit */
  262.          tim[max] = firstx + deltax * ((float) max);
  263.          max = max + 1;
  264.          if(max >= _MAXSPCLEN) {
  265.             printf("Warning, spectrum is not fully read, INCREASE MAXSPCLEN\n");
  266.             fclose(fp); free(s); free(z); return(max);
  267.          }
  268.       }
  269.    }
  270.    fclose(fp);
  271.    free(s); free(z);
  272.    return(max);
  273. }
  274.  
  275.  
  276.  
  277.  
  278.